package com.whh.tq.common;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

/**
 * 提交到12306.com的数据封装类
 * 
 * @author wanghh
 * 
 */
public class ReqParamHelper {
	
	// 余票查询验证码请求地址
	public static String URL_RANDCODE = "http://dynamic.12306.cn/TrainQuery/passCodeAction.do?rand=rrand";
	// 余票查询表单提交地址
	public static String URL_TICKETLEFT_QUERY = "http://dynamic.12306.cn/TrainQuery/iframeLeftTicketByStation.jsp";
	// 时刻表查询表单提交地址
	public static String URL_FROM_TO_QUERY = "http://dynamic.12306.cn/TrainQuery/iframeTrainInfoByStation.jsp";
	// 时刻表按车次查询表单提交地址
	public static String URL_TRAINCODE_QUERY= "http://dynamic.12306.cn/TrainQuery/iframeTrainPassStationByTrainCode.jsp";
	// 车次查询的referer
	public static String REFERER_TRAINCODE = "http://dynamic.12306.cn/TrainQuery/trainPassStationByTrainCode.jsp";
	// 站点查询的referer
	public static String REFERER_STATION ="http://dynamic.12306.cn/TrainQuery/trainPassStationByTrainCode.jsp";
	// 验证码
	public static String PARAM_RANDCODE = "randCode";

	// 余票查询发到站
	public static String PARAM_FROM_STATION_TICKETLEFT = "startStation_ticketLeft";
	public static String PARAM_FROM_STATION_TICKETLEFT_NEW_VALUE = "startStation_ticketLeft_new_value";
	public static String PARAM_TO_STATION_TICKETLEFT = "arriveStation_ticketLeft";
	public static String PARAM_TO_STATION_TICKETLEFT_NEW_VALUE = "arriveStation_ticketLeft_new_value";

	// 时刻表查询发到站
	public static String PARAM_FROM_STATION = "startStation";
	public static String PARAM_FROM_STATION_NEW_VALUE = "startStation_new_value";
	public static String PARAM_TO_STATION = "arriveStation";
	public static String PARAM_TO_STATION_NEW_VALUE = "arriveStation_new_value";
	
	public static String PARAM_FDL = "fdl";
	
	// 必填，每天会变，参数名、值随机
	public static String PARAM_ICT = "";
	public static String VALUE_ICT = "";

	// 查询类型，普通查询00;学生团体查询0X；农民工团体查询0M
	public static String PARAM_LX = "lx";
	public static String VALUE_LX_00 = "00";
	public static String VALUE_LX_0X = "0X";
	public static String VALUE_LX_0M = "0M";

	// 余票查询日期
	public static String PARAM_NDAY_3 = "nday3";
	public static String PARAM_NDAY_3_NEW_VALUE = "nday3_new_value";
	public static String PARAM_NMONTH_3 = "nmonth3";
	public static String PARAM_NMONTH_3_NEW_VALUE = "nmonth3_new_value";

	// 时刻表查询日期
	public static String PARAM_NDAY_1 = "nday1";
	public static String PARAM_NDAY_1_NEW_VALUE = "nday1_new_value";
	public static String PARAM_NMONTH_1 = "nmonth1";
	public static String PARAM_NMONTH_1_NEW_VALUE = "nmonth1_new_value";
	
	// 车次
	public static String PARAM_TRAINCODE = "trainCode";
	public static String PARAM_TRAINCODE_NEW_VALUE = "trainCode_new_value";

	// 查询条件，全部1；始发3；终到4；始发终到5；过路6；
	public static String PARAM_RFLAG = "rFlag";
	public static String VALUE_RFLAG_ALL = "1";
	public static String VALUE_RFLAG_START = "3";
	public static String VALUE_RFLAG_END = "4";
	public static String VALUE_RFLAG_START_END = "5";
	public static String VALUE_RFLAG_PASS = "6";

	// 是否选中全部火车类型，如果没有全部post信息不填加此选项
	public static String PARAM_NAME_CKBALL = "name_ckball";
	public static String VALUE_NAME_CKBALL = "value_ckball";

	// 火车类型，分别为动车/快速/临客/普快/普客/特快/直特，如没有选不添加post数据中
	public static String PARAM_TFLAG_DC = "tFlagDC";
	public static String PARAM_TFLAG_K = "tFlagK";
	public static String PARAM_TFLAG_LK = "tFlagLK";
	public static String PARAM_TFLAG_PK = "tFlagPK";
	public static String PARAM_TFLAG_PKE = "tFlagPKE";
	public static String PARAM_TFLAG_T = "tFlagT";
	public static String PARAM_TFLAG_Z = "tFlagZ";
	public static String VALUE_TFLAG_DC = "DC";
	public static String VALUE_TFLAG_K = "K";
	public static String VALUE_TFLAG_LK = "LK";
	public static String VALUE_TFLAG_PK = "PK";
	public static String VALUE_TFLAG_PKE = "PKE";
	public static String VALUE_TFLAG_T = "T";
	public static String VALUE_TFLAG_Z = "Z";

	/**
	 * 获取余票查询所需参数,目前查询所有，没有过滤
	 * @param randCode
	 * @param month
	 * @param day
	 * @param fromStation
	 * @param toStation
	 * @param trainCode
	 * @return
	 */
	public static List<BasicNameValuePair> getDefaultLeftTicketPostData(String randCode,String month,String day,String fromStation,String toStation,String trainCode,String ictParam,String ictValue){
		List<BasicNameValuePair> postData = new ArrayList<BasicNameValuePair>();
		postData.add(new BasicNameValuePair(PARAM_RANDCODE, randCode));
		postData.add(new BasicNameValuePair(PARAM_FROM_STATION_TICKETLEFT, encodeStation(fromStation)));
		postData.add(new BasicNameValuePair(PARAM_FROM_STATION_TICKETLEFT_NEW_VALUE, "true"));
		postData.add(new BasicNameValuePair(PARAM_TO_STATION_TICKETLEFT, encodeStation(toStation)));
		postData.add(new BasicNameValuePair(PARAM_TO_STATION_TICKETLEFT_NEW_VALUE, "true"));
		postData.add(new BasicNameValuePair(PARAM_NMONTH_3, month));
		postData.add(new BasicNameValuePair(PARAM_NMONTH_3_NEW_VALUE, "true"));
		postData.add(new BasicNameValuePair(PARAM_NDAY_3, day));
		postData.add(new BasicNameValuePair(PARAM_NDAY_3_NEW_VALUE, "true"));
		postData.add(new BasicNameValuePair(PARAM_TRAINCODE, trainCode));
		postData.add(new BasicNameValuePair(PARAM_TRAINCODE_NEW_VALUE, "true"));
		postData.add(new BasicNameValuePair(PARAM_NAME_CKBALL, VALUE_NAME_CKBALL));
		postData.add(new BasicNameValuePair(PARAM_RFLAG, VALUE_RFLAG_ALL));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_DC, VALUE_TFLAG_DC));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_K, VALUE_TFLAG_K));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_LK, VALUE_TFLAG_LK));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_PK, VALUE_TFLAG_PK));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_PKE, VALUE_TFLAG_PKE));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_T, VALUE_TFLAG_T));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_Z, VALUE_TFLAG_Z));
		postData.add(new BasicNameValuePair(PARAM_LX, VALUE_LX_00));
		postData.add(new BasicNameValuePair(PARAM_FDL, ""));
		postData.add(new BasicNameValuePair(ictParam, ictValue));
		return postData;
	}
	
	/**
	 * 获取时刻表查询参数
	 * @param randCode
	 * @param month
	 * @param day
	 * @param fromStation
	 * @param toStation
	 * @param trainCode
	 * @return
	 */
	public static List<BasicNameValuePair> getDefaultFromToPostData(String randCode,String month,String day,String fromStation,String toStation){
		List<BasicNameValuePair> postData = new ArrayList<BasicNameValuePair>();
		postData.add(new BasicNameValuePair(PARAM_RANDCODE, randCode));
		postData.add(new BasicNameValuePair(PARAM_FROM_STATION, (fromStation)));
		postData.add(new BasicNameValuePair(PARAM_FROM_STATION_NEW_VALUE, "true"));
		postData.add(new BasicNameValuePair(PARAM_TO_STATION, (toStation)));
		postData.add(new BasicNameValuePair(PARAM_TO_STATION_NEW_VALUE, "true"));
		postData.add(new BasicNameValuePair(PARAM_NMONTH_1, month));
		postData.add(new BasicNameValuePair(PARAM_NMONTH_1_NEW_VALUE, "true"));
		postData.add(new BasicNameValuePair(PARAM_NDAY_1, day));
		postData.add(new BasicNameValuePair(PARAM_NDAY_1_NEW_VALUE, "true"));
		postData.add(new BasicNameValuePair(PARAM_TRAINCODE_NEW_VALUE, "true"));
		postData.add(new BasicNameValuePair(PARAM_NAME_CKBALL, VALUE_NAME_CKBALL));
		postData.add(new BasicNameValuePair(PARAM_RFLAG, VALUE_RFLAG_ALL));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_DC, VALUE_TFLAG_DC));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_K, VALUE_TFLAG_K));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_LK, VALUE_TFLAG_LK));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_PK, VALUE_TFLAG_PK));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_PKE, VALUE_TFLAG_PKE));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_T, VALUE_TFLAG_T));
		postData.add(new BasicNameValuePair(PARAM_TFLAG_Z, VALUE_TFLAG_Z));
		postData.add(new BasicNameValuePair(PARAM_LX, VALUE_LX_00));
		return postData;
	}
	
	/**
	 * 组装获取车次的url
	 * @param year
	 * @param month
	 * @param day
	 * @param traincode
	 * @return
	 */
	public static String getTrainCodeGetDataUrl(String year,String month,String day,String trainCode){
		return "http://dynamic.12306.cn/TrainQuery/skbcx.jsp?cxlx=cc&date="+year+month+day+"&trainCode="+trainCode.toUpperCase();
	}
	
	/**
	 * 组装获取车次的url
	 * @param year
	 * @param month
	 * @param day
	 * @param station
	 * @return
	 */
	public static String getStationGetDataUrl(String year,String month,String day,String station){
		return "http://dynamic.12306.cn/TrainQuery/skbcx.jsp?cxlx=czjgcc&zm=&date="+year+month+day+"&stationName_passTrain="+station;
	}
	
	/**
	 * 发到站站名加密，由js版本修改而来
	 * @param str
	 * @return
	 */
	public static String encodeStation(String str) {
		String pwd = "liusheng";
		String prand = "";
		for (int i = 0; i < pwd.length(); i++) {
			prand += (int) pwd.charAt(i);
		}
		int sPos = (int) Math.floor(prand.length() / 5);
		int mult = Integer.parseInt("" + prand.charAt(sPos)
				+ prand.charAt(sPos * 2) + prand.charAt(sPos * 3)
				+ prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
		int incr = (int) Math.ceil(pwd.length() / 2);
		int modu = (int) (Math.pow(2, 31) - 1);
		String salt = Math.round(Math.random() * 1000000000) % 100000000 + "";
		prand += salt;
		prand = (mult * prand.length() + incr) % modu + "";
		int enc_chr;
		String enc_str = "";
		for (int i = 0; i < str.length(); i++) {
			enc_chr = Integer.parseInt(((int) str.charAt(i) ^ (int) Math
					.floor((Double.parseDouble(prand) / modu) * 255)) + "");
			if (enc_chr < 16) {
				enc_str += "0" + Integer.toHexString(enc_chr);
			} else {
				enc_str += Integer.toHexString(enc_chr);
			}
			prand = (mult * Integer.parseInt(prand) + incr) % modu + "";
		}
		salt = Integer.toHexString(Integer.parseInt(salt));
		while (salt.length() < 8) {
			salt = "0" + salt;
		}
		enc_str += salt;
		return enc_str;
	}
}
